﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DIProject.Dictionary
{
    public partial class Goods : System.Windows.Forms.Form// Form
    {
        public Goods()
        {
            InitializeComponent();
        }
        protected DataSets.Dictionary.GoodsDataTable Data
        {
            get
            {
                this.dictionary.Goods.DefaultView.Sort = "Name";
                return this.dictionary.Goods;
            }
        }
        private void Goods_Load(object sender, EventArgs e)
        {
          //  this.SuspendLayout();
            this.Cursor = Cursors.WaitCursor;
            this.goodsTableAdapter.Fill(this.dictionary.Goods, DIProject.Data.Sync.IsAdmin);
            this.dgvGoods.RowCount = this.Data.DefaultView.Count;
            this.Cursor = Cursors.Default;
           // this.ResumeLayout();
        }

        private void dgvGoods_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            
            dgvGoods.Rows[e.RowIndex].Selected = true;
            DataRowView drv = this.Data.DefaultView[e.RowIndex];
            DataSets.Dictionary.GoodsRow gr = drv.Row as DataSets.Dictionary.GoodsRow;
           // bool f = !gr.IsParentIdNull();
            ShowDetails(Util.ActionType.Update, this.dictionary.Goods, gr.Id,false);
            
        }
        private void ShowDetails(Util.ActionType actType, DataSets.Dictionary.GoodsDataTable data, Int32 id,bool isDiv)
        {
            DIProject.Dictionary.GoodsDetails gd = DIProject.Dictionary.GoodsDetails.GoodsDetailsCache;
            if (gd == null)
            {
                gd = new GoodsDetails(actType, data, id, isDiv);
                DIProject.Dictionary.GoodsDetails.GoodsDetailsCache = gd;
                gd.Show(this);
            }
            else
            {
                gd.MyShow(actType, data, id,isDiv);
            }
            dgvGoods.Refresh();
        }
        
        private void ShowDetails(Util.ActionType actType, DataSets.Dictionary.GoodsDataTable data, Int32 id)
        {
            DIProject.Dictionary.GoodsDetails gd = DIProject.Dictionary.GoodsDetails.GoodsDetailsCache;
            if (gd == null)
            {
                gd = new GoodsDetails(actType, data, id);
                DIProject.Dictionary.GoodsDetails.GoodsDetailsCache = gd;
                gd.Show(this);
            }
            else
            {
                gd.MyShow(actType, data, id);
            }
            dgvGoods.Refresh();
        }
        /// <summary>
        /// стираем весь кеш что бы в следущий раз перечитать
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            this.goodsTableAdapter.Fill(this.dictionary.Goods, DIProject.Data.Sync.IsAdmin);
            GoodsDetails.GoodsDetailsCache = null;
            DIProject.Data.Assortment.ClearData();
            new DIProject.Data.Test2().ClearData();
            DIProject.Data.Goods.ClearData();
            DIProject.Data.Country.ClearData();
            this.Cursor = Cursors.Default;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            lock (this)
            {
                this.Cursor = Cursors.WaitCursor;
                this.ShowDetails(DIProject.Util.ActionType.Insert, this.dictionary.Goods, Int32.MinValue);
                this.Cursor = Cursors.Default;
            }
        }
/// <summary>
/// деление товара
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            if (dgvGoods.SelectedRows.Count < 1)
            {
                MessageBox.Show("Требуется выбрать товар");
                return;
            }
           // DataGridViewRow dgrv = dgvGoods.SelectedRows[0];
            Int32 iTmp = (Int32)dgvGoods.SelectedRows[0].Cells["idDataGridViewTextBoxColumn"].Value;
            DataSets.Dictionary.GoodsRow gr = this.Data.FindById(iTmp);
            if (gr == null)
            {
                MessageBox.Show("Не удалось найти товар");
                return;
            }
            GoodsDiv gd = new GoodsDiv(gr, this.dictionary.Goods);
            DialogResult dr = gd.ShowDialog();
            if (dr == DialogResult.OK)
            {
                dgvGoods.Rows.Clear();
                dgvGoods.RowCount = this.Data.DefaultView.Count;
            }
        }
        /// <summary>
        /// поиск товара
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txbFilter.Text))
            {
                this.Data.DefaultView.RowFilter = string.Empty;
                this.dgvGoods.Rows.Clear();
                this.dgvGoods.RowCount = this.Data.DefaultView.Count;
            }
            else
            {
                this.Data.DefaultView.RowFilter = "Name like '*" + txbFilter.Text + "*'";
                this.dgvGoods.Rows.Clear();
                this.dgvGoods.RowCount = this.Data.DefaultView.Count;
            }

        }

        /// <summary>
        /// заполняем ячейки грида по требованию.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvGoods_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
        {
            if (this.Data.DefaultView.Count < 1)
                return;
            Int32 cInd = e.ColumnIndex;
            Int32 rInd = e.RowIndex;
            string dpn = dgvGoods.Columns[cInd].DataPropertyName;
            e.Value = this.Data.DefaultView[rInd][dpn];
        }
    }
}